package com.wits.qso.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.wits.platform.dao.impl.BaseDaoImpl;
import com.wits.qso.dao.custom.HomeworkDaoCustom;
import com.wits.qso.entity.Homework;

/**
 * 作业数据操作实现
 */
@Component
public class HomeworkDaoImpl extends BaseDaoImpl<Homework, String> implements
		HomeworkDaoCustom {
	@SuppressWarnings("unchecked")
	public List<Homework> findHomeworks(Map<String, Object> param) {

		if (param == null) {
			return new ArrayList<Homework>();
		}
		StringBuffer buf = new StringBuffer();
		buf.append(" SELECT HWK.hwk_id, HWK.stu_id, HWK.ter_id, HWK.course_id, ");
		buf.append(" HWK.group_id, HWK.hwkh_id,HWK.hwk_title, HWK.hwk_desc, ");
		buf.append(" HWK.date_handin, HWK.date_correct, HWK.hwk_grade, HWK.hwk_cmmt, ");
		buf.append(" HWK.hwk_status, HWK.remark, HWK.user_cre, HWK.date_cre, ");
		buf.append(" HWK.user_upd, HWK.date_upd,HWKH.hwkh_title,TEAC.ter_name,  ");
		buf.append(" DEPT.dept_title,GRAD.grade_title, CLAS.cls_title, ");
		buf.append(" COUR.course_title,STUD.stu_name ");
		buf.append(" FROM QSO_HOMEWORK HWK LEFT JOIN QSO_HOMEWORK_H HWKH ON HWK.HWKH_ID=HWKH.HWKH_ID ");
		buf.append(" LEFT JOIN QSO_CRECONTENT CREC ON HWKH.HWKH_ID=CREC.CRECNT_HWKHID ");
		buf.append(" LEFT JOIN QSO_BTABLE BTAB ON CREC.CRECNT_BTID=BTAB.BTABLE_ID ");
		buf.append(" LEFT JOIN QSO_DEPT DEPT ON DEPT.DEPT_ID=BTAB.BTABLE_DPTID ");
		buf.append(" LEFT JOIN QSO_GRADE GRAD ON GRAD.GRADE_ID=BTAB.BTABLE_GRADEID ");
		buf.append(" LEFT JOIN QSO_CLASS CLAS ON CLAS.CLS_ID=BTAB.BTABLE_CLAID ");
		buf.append(" LEFT JOIN QSO_COURSE COUR ON COUR.COURSE_ID=BTAB.BTABLE_COUID ");
		buf.append(" LEFT JOIN QSO_TEACHER TEAC ON TEAC.TER_ID=BTAB.BTABLE_TEAID ");
		buf.append(" LEFT JOIN QSO_STUDENT STUD ON STUD.STU_ID=HWK.STU_ID WHERE 1=1 ");

		if (param.get("hwk_id") != null) {
			buf.append(" and HWK.hwk_id=:hwk_id ");
		}
		if (param.get("hwk_title") != null) {
			buf.append(" and HWK.hwk_title like %:hwk_title% ");
		}
		if (param.get("date_handin") != null) {
			buf.append(" and HWK.date_handin =:date_handin ");
		}
		if (param.get("date_correct") != null) {
			buf.append(" and HWK.date_correct =:date_correct ");
		}
		if (param.get("hwk_status") != null) {
			buf.append(" and HWK.hwk_status =:hwk_status ");
		}
		if (param.get("ter_name") != null) {
			buf.append(" and TEAC.ter_name like %:ter_name% ");
		}
		if (param.get("DEPT_TITLE") != null) {
			buf.append(" and DEPT.DEPT_TITLE like %:DEPT_TITLE% ");
		}
		if (param.get("GRADE_TITLE") != null) {
			buf.append(" and GRAD.GRADE_TITLE like %:GRADE_TITLE% ");
		}
		if (param.get("CLS_TITLE") != null) {
			buf.append(" and CLAS.CLS_TITLE like %:CLS_TITLE% ");
		}
		if (param.get("COURSE_TITLE") != null) {
			buf.append(" and COUR.COURSE_TITLE like %:COURSE_TITLE% ");
		}
		if (param.get("STU_NAME") != null) {
			buf.append(" and STUD.STU_NAME like %:STU_NAME% ");
		}
		

		Query a = em.createNativeQuery(buf.toString(), Homework.class);
		if (param.get("HWK_ID") != null) {
			a.setParameter("HWK_ID", param.get("HWK_ID"));
		}
		if (param.get("HWK_TITLE") != null) {
			a.setParameter("HWK_TITLE", param.get("HWK_TITLE"));
		}
		if (param.get("DATE_HANDIN") != null) {
			a.setParameter("DATE_HANDIN", param.get("DATE_HANDIN"));
		}
		if (param.get("DATE_CORRECT") != null) {
			a.setParameter("DATE_CORRECT", param.get("DATE_CORRECT"));
		}
		if (param.get("HWK_STATUS") != null) {
			a.setParameter("HWK_STATUS", param.get("HWK_STATUS"));
		}
		if (param.get("TER_NAME") != null) {
			a.setParameter("TER_NAME", param.get("TER_NAME"));
		}
		if (param.get("DEPT_TITLE") != null) {
			a.setParameter("DEPT_TITLE", param.get("DEPT_TITLE"));
		}
		if (param.get("GRADE_TITLE") != null) {
			a.setParameter("GRADE_TITLE", param.get("GRADE_TITLE"));
		}
		if (param.get("CLS_TITLE") != null) {
			a.setParameter("CLS_TITLE", param.get("CLS_TITLE"));
		}
		if (param.get("COURSE_TITLE") != null) {
			a.setParameter("COURSE_TITLE", param.get("COURSE_TITLE"));
		}
		if (param.get("STU_NAME") != null) {
			a.setParameter("STU_NAME", param.get("STU_NAME"));
		}
		
		@SuppressWarnings("rawtypes")
		List list = a.getResultList();
		return list;
	}

	public boolean modifyStatus(String id, String status) {
//		boolean res=false;
//		try {
//			String sql = " update qso_homework set hwk_status=:hwk_status where hwk_id=:hwk_id ";
//			Query query = em.createNativeQuery(sql);
//			query.setParameter("hwk_id", id);
//			query.setParameter("hwk_status", status);
//
//			query.executeUpdate();
//			res=true;
//		} catch (Exception e) {
//			e.printStackTrace();
//			res=false;
//		}
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("hwk_id", id);
		param.put("hwk_status", status);
		param.put("date_handin", new Date());
		return modifyFeild(param);
	}

	public boolean modifyFeild(Map<String, Object> param) {
		boolean res=false;
		//字段计数器
		int count=0;
		try {
			StringBuffer buf=new StringBuffer();
			buf.append(" update qso_homework set ");
			if(param==null||param.size()==0||(param.containsKey("hwk_id")&&param.size()==1)){
				throw new RuntimeException("can not find columns to update from given params!");
			}
			if(param.get("hwk_status")!=null){
				count++;
				if(count>1){
					buf.append(",");
				}
				buf.append("hwk_status=:hwk_status");
			}
			if(param.get("hwk_desc")!=null){
				count++;
				if(count>1){
					buf.append(",");
				}
				buf.append("hwk_desc=:hwk_desc");
			}
			if(param.get("hwk_cmmt")!=null){
				count++;
				if(count>1){
					buf.append(",");
				}
				buf.append("hwk_cmmt=:hwk_cmmt");
			}
			if(param.get("hwk_grade")!=null){
				count++;
				if(count>1){
					buf.append(",");
				}
				buf.append("hwk_grade=:hwk_grade");
			}
			if(param.get("date_handin")!=null){
				count++;
				if(count>1){
					buf.append(",");
				}
				buf.append("date_handin=:date_handin");
			}
			if(param.get("date_correct")!=null){
				count++;
				if(count>1){
					buf.append(",");
				}
				buf.append("date_correct=:date_correct");
			}
			buf.append(" where hwk_id=:hwk_id ");
//			String sql = " update qso_homework set hwk_status=:hwk_status,hwk_desc=:hwk_desc,hwk_cmmt=:hwk_cmmt,hwk_grade=:hwk_grade where hwk_id=:hwk_id ";
			
			String sql = buf.toString();
			Query query = em.createNativeQuery(sql);
			query.setParameter("hwk_id", param.get("hwk_id"));
//			query.setParameter("hwk_status", param.get("hwk_status"));
//			query.setParameter("hwk_desc", param.get("hwk_desc"));
//			query.setParameter("hwk_cmmt", param.get("hwk_cmmt"));
//			query.setParameter("hwk_grade", param.get("hwk_grade"));
			if(param.get("hwk_status")!=null){
				query.setParameter("hwk_status", param.get("hwk_status"));
			}
			if(param.get("hwk_desc")!=null){
				query.setParameter("hwk_desc", param.get("hwk_desc"));
			}
			if(param.get("hwk_cmmt")!=null){
				query.setParameter("hwk_cmmt", param.get("hwk_cmmt"));
			}
			if(param.get("hwk_grade")!=null){
				query.setParameter("hwk_grade", param.get("hwk_grade"));
			}
			if(param.get("date_handin")!=null){
				query.setParameter("date_handin", param.get("date_handin"));
			}
			if(param.get("date_correct")!=null){
				query.setParameter("date_correct", param.get("date_correct"));
			}
			
			query.executeUpdate();
			res=true;
		} catch (Exception e) {
			e.printStackTrace();
			res=false;
		}finally{
			count=0;
		}
		return res;
	}

	public static void main(String[] args){
		
	}
}
